package com.example.demo03.web;


import java.util.List;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.example.demo03.entity.response.TodoItemAPI;
import com.example.demo03.service.TodoItemService;


/**
 * The TodoItemController class is a RESTful controller that handles HTTP requests related to TodoItem operations.
 * It maps incoming HTTP requests to service methods and returns appropriate HTTP responses.
 * This controller interacts with the TodoItemService to perform business logic.
 *
 * @author [Your Name]
 * @version 1.0
 * @since [Date]
 * @Api(value = "TodoItem API", description = "Operations related to TodoItem management")
 */
@RestController
@RequestMapping("/api/todos")
@Api(value = "TodoItem API", description = "Operations related to TodoItem management")
public class TodoItemController {


    /**
     * The TodoItemService instance used to perform business logic related to TodoItems.
     * It is injected via Spring's dependency injection mechanism.
     */
    @Autowired
    private TodoItemService todoItemService;


    /**
     * Handles HTTP GET requests to retrieve all TodoItems.
     * Converts the result from the service layer into a ResponseEntity with an appropriate HTTP status.
     *
     * @return a ResponseEntity containing a list of TodoItemAPI objects with HTTP status 200 (OK)
     * @ApiOperation(value = "Get all TodoItems", notes = "Retrieve all TodoItems", response = TodoItemAPI.class, responseContainer = "List")
     * @ApiResponses(value = {
     *     @ApiResponse(code = 200, message = "Successfully retrieved all TodoItems")
     * })
     */
    @GetMapping
    @ApiOperation(value = "Get all TodoItems", notes = "Retrieve all TodoItems", response = TodoItemAPI.class, responseContainer = "List")
    @ApiResponses(value = {
          @ApiResponse(code = 200, message = "Successfully retrieved all TodoItems")
    })
    public ResponseEntity<List<TodoItemAPI>> getAll() {
        List<TodoItemAPI> todoItems = todoItemService.getAll();
        return new ResponseEntity<>(todoItems, HttpStatus.OK);
    }


    /**
     * Handles HTTP GET requests to retrieve a single TodoItem by its id.
     * Returns a ResponseEntity with the TodoItemAPI object if found, or 404 (NOT_FOUND) if not found.
     *
     * @param id the unique identifier of the TodoItem to retrieve
     * @return a ResponseEntity with the TodoItemAPI object and HTTP status 200 (OK) if found,
     *         or HTTP status 404 (NOT_FOUND) if not found
     * @ApiOperation(value = "Get TodoItem by id", notes = "Retrieve a TodoItem by its unique id", response = TodoItemAPI.class)
     * @ApiResponses(value = {
     *     @ApiResponse(code = 200, message = "Successfully retrieved the TodoItem"),
     *     @ApiResponse(code = 404, message = "TodoItem not found")
     * })
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "Get TodoItem by id", notes = "Retrieve a TodoItem by its unique id", response = TodoItemAPI.class)
    @ApiResponses(value = {
          @ApiResponse(code = 200, message = "Successfully retrieved the TodoItem"),
          @ApiResponse(code = 404, message = "TodoItem not found")
    })
    public ResponseEntity<TodoItemAPI> getById(@PathVariable Long id) {
        TodoItemAPI todoItem = todoItemService.getById(id);
        return todoItem!= null? new ResponseEntity<>(todoItem, HttpStatus.OK) : new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }


    /**
     * Handles HTTP POST requests to create a new TodoItem.
     * Converts the request body (TodoItemAPI) into a service call and returns the result
     * in a ResponseEntity with HTTP status 201 (CREATED).
     *
     * @param todoItemAPI the TodoItemAPI object to be created, provided in the request body
     * @return a ResponseEntity with the number of affected rows and HTTP status 201 (CREATED)
     * @ApiOperation(value = "Create a new TodoItem", notes = "Create a new TodoItem", response = Integer.class)
     * @ApiResponses(value = {
     *     @ApiResponse(code = 201, message = "TodoItem created successfully")
     * })
     */
    @PostMapping
    @ApiOperation(value = "Create a new TodoItem", notes = "Create a new TodoItem", response = Integer.class)
    @ApiResponses(value = {
          @ApiResponse(code = 201, message = "TodoItem created successfully")
    })
    public ResponseEntity<Integer> create(@RequestBody TodoItemAPI todoItemAPI) {
        int result = todoItemService.create(todoItemAPI);
        return new ResponseEntity<>(result, HttpStatus.CREATED);
    }


    /**
     * Handles HTTP PUT requests to update an existing TodoItem.
     * Converts the request body (TodoItemAPI) into a service call and returns the result
     * in a ResponseEntity with HTTP status 200 (OK).
     *
     * @param todoItemAPI the TodoItemAPI object with updated information, provided in the request body
     * @return a ResponseEntity with the number of affected rows and HTTP status 200 (OK)
     * @ApiOperation(value = "Update an existing TodoItem", notes = "Update an existing TodoItem", response = Integer.class)
     * @ApiResponses(value = {
     *     @ApiResponse(code = 200, message = "TodoItem updated successfully")
     * })
     */
    @PutMapping
    @ApiOperation(value = "Update an existing TodoItem", notes = "Update an existing TodoItem", response = Integer.class)
    @ApiResponse(code = 200, message = "TodoItem updated successfully")
    public ResponseEntity<Integer> update(@RequestBody TodoItemAPI todoItemAPI) {
        int result = todoItemService.update(todoItemAPI);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


    /**
     * Handles HTTP DELETE requests to delete a TodoItem by its id.
     * Converts the id from the path variable into a service call and returns the result
     * in a ResponseEntity with HTTP status 200 (OK).
     *
     * @param id the unique identifier of the TodoItem to delete, provided in the path variable
     * @return a ResponseEntity with the number of affected rows and HTTP status 200 (OK)
     * @ApiOperation(value = "Delete a TodoItem by id", notes = "Delete a TodoItem by its unique id", response = Integer.class)
     * @ApiResponses(value = {
     *     @ApiResponse(code = 200, message = "TodoItem deleted successfully")
     * })
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "Delete a TodoItem by id", notes = "Delete a TodoItem by its unique id", response = Integer.class)
    @ApiResponses(value = {
          @ApiResponse(code = 200, message = "TodoItem deleted successfully")
    })
    public ResponseEntity<Integer> deleteById(@PathVariable Long id) {
        int result = todoItemService.deleteById(id);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


    /**
     * Handles HTTP GET requests to retrieve the top 10 TodoItems ordered by score.
     * Converts the result from the service layer into a ResponseEntity with an appropriate HTTP status.
     *
     * @return a ResponseEntity containing a list of the top 10 TodoItemAPI objects with HTTP status 200 (OK)
     * @ApiOperation(value = "Get top 10 TodoItems by score", notes = "Retrieve the top 10 TodoItems ordered by score", response = TodoItemAPI.class, responseContainer = "List")
     * @ApiResponses(value = {
     *     @ApiResponse(code = 200, message = "Successfully retrieved the top 10 TodoItems")
     * })
    */
    @GetMapping("/top10")
    @ApiOperation(value = "Get top 10 TodoItems by score", notes = "Retrieve the top 10 TodoItems ordered by score", response = TodoItemAPI.class, responseContainer = "List")
    @ApiResponses(value = {
          @ApiResponse(code = 200, message = "Successfully retrieved the top 10 TodoItems")
    })
    public ResponseEntity<List<TodoItemAPI>> getTop10ByScore() {
        List<TodoItemAPI> top10 = todoItemService.getTop10ByScore();
        return new ResponseEntity<>(top10, HttpStatus.OK);
    }
}